package com.cdhospital.congfig;

import com.cdhospital.Model._MappingKit;
import com.cdhospital.handler.HtmlHandler;
import com.cdhospital.intercept.LogInterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.jfplugin.mail.MailKit;
import com.jfplugin.mail.MailPlugin;

/**
 * API引导式配置
 */
public class MainConfig extends JFinalConfig {

	public static void main(String[] args) {
		/**
		 * 特别注意：本项目采用jetty 方式进行启动
		 * 
		 */
		JFinal.start("src/main/webapp", 80, "/", 5);

	}

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", true));
       me.setError404View("/404.html");
       me.setError500View("/500.html");
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add(new AdminRoutes()); // 后端路由
		me.add(new WebsiteRoutes()); // 前端路由
		
	}

	/**
	 * 配置模板
	 * */
	public void configEngine(Engine me) {
		me.addSharedFunction("/WEB-INF/view/common/newsform.html");
		me.addSharedFunction("/WEB-INF/view/common/left.html");
		me.addSharedFunction("/WEB-INF/view/common/top.html");
		me.addSharedFunction("/WEB-INF/view/common/_paginate.html");
		me.addSharedFunction("/WEB-INF/view/common/_paginates.html");
		me.addSharedFunction("/WEB-INF/view/common/info.html");
        
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置Druid数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
				PropKit.get("user"), PropKit.get("password").trim());
		me.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		// 配置缓存插件
		me.add(new EhCachePlugin());	
		
		me.add(arp);
		
	}

	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
				PropKit.get("password").trim());
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new LogInterceptor());
		me.add(new SessionInViewInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new HtmlHandler());
	}

	@Override
	public void beforeJFinalStop() {
		// TODO Auto-generated method stub
		System.out.println("停止");
	}

	@Override
	public void afterJFinalStart() {
		// TODO Auto-generated method stub
		new MailPlugin(PropKit.use("mail.properties").getProperties()).start();
		System.err.println("启动");
       
	}
}
